package com.zhanghe.study.java8.dll;

import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    static {
        System.setProperty("jna.library.path","/Users/zhanghe/Desktop/user/myself/GitProject/java8/src/main/resources");
    }
    public static byte[] getBytesByFile(String filePath) {
        try {
            File file=new File(filePath);
            //获取输入流
            FileInputStream fis = new FileInputStream(file);

            //新的 byte 数组输出流，缓冲区容量1024byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            //缓存
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            //改变为byte[]
            byte[] data = bos.toByteArray();
            //
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws FileNotFoundException {

        String path = "/Users/zhanghe/Desktop/素材示例/9392b83dc43a45ecb13237e5bf67648f.jpeg";

        // 初始化数据
        int detid = 0;
        byte[] inJpg = getBytesByFile(path);
        List<TCLineInfo> tclineInfos = new ArrayList<>();
        byte[] outJpg = new byte[0];

        // 创建检测器
        int createResult = DetTclineLibrary.INSTANCE.CreateDetector();
        System.out.println("CreateDetector result: " + createResult);

        if (createResult != 0) {
            return;
        }

        // 准备输入数据
        Pointer inJpgPtr = VectorConverter.toPointer(inJpg);
        IntByReference tclineInfosSizeRef = new IntByReference(tclineInfos.size());
        Pointer tclineInfosPtr = VectorConverter.toPointer(tclineInfos);
        IntByReference outJpgSizeRef = new IntByReference(outJpg.length);
        Pointer outJpgPtr = VectorConverter.toPointer(outJpg);

        // 调用DetTCLine方法
        int detResult = DetTclineLibrary.INSTANCE.DetTCLine(detid, inJpgPtr, tclineInfosSizeRef, tclineInfosPtr, outJpgSizeRef, outJpgPtr);
        System.out.println("DetTCLine result: " + detResult);

        // 释放内存
        if (inJpgPtr != null) {
            inJpgPtr.clear(inJpg.length);
        }
        if (tclineInfosPtr != null) {
            tclineInfosPtr.clear(tclineInfos.size() * TCLineInfo.SIZE);
        }
        if (outJpgPtr != null) {
            outJpgPtr.clear(outJpg.length);
        }

        // 读取输出数据
        int tclineInfosSize = tclineInfosSizeRef.getValue();
        tclineInfos = VectorConverter.fromPointer(tclineInfosPtr, tclineInfosSize);
        int outJpgSize = outJpgSizeRef.getValue();
        outJpg = VectorConverter.fromPointerByte(outJpgPtr, outJpgSize);

        // 释放检测器
        int releaseResult = DetTclineLibrary.INSTANCE.ReleaseDetector(detid);
        System.out.println("ReleaseDetector result: " + releaseResult);

        // 输出结果
        System.out.println("tclineInfos: " + tclineInfos);
        System.out.println("outJpg: " + outJpg);
    }
}